/*
 * 描          述:  <描述>
 * 修  改   人:  PengQingyang
 * 修改时间:  2022年4月22日
 * <修改描述:>
 */
package com.tx.component.social.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import javax.annotation.Resource;

import com.tx.component.plugin.context.PluginContext;
import com.tx.component.plugin.model.PluginInstance;
import com.tx.component.plugin.service.PluginInstanceService;
import com.tx.component.social.SocialLoginPlugin;
import com.tx.component.social.model.SocialAccountType;
import com.tx.component.social.service.SocialLoginPluginService;

/**
 * 第三方登陆插件业务层<br/>
 * <功能详细描述>
 * 
 * @author  PengQingyang
 * @version  [版本号, 2022年4月22日]
 * @see  [相关类/方法]
 * @since  [产品/模块版本]
 */
public class SocialLoginPluginServiceImpl implements SocialLoginPluginService {
    
    @Resource
    private PluginInstanceService pluginInstanceService;
    
    /**
     * @return
     */
    @Override
    public List<SocialLoginPlugin<?>> getSocialLoginPluginList() {
        Map<String, Object> params = new HashMap<>();
        params.put("category", "login");
        params.put("valid", "true");
        List<PluginInstance> piList = this.pluginInstanceService.queryList(true,
                params);
        List<SocialLoginPlugin<?>> pluginList = piList.stream()
                .map(pi -> (SocialLoginPlugin<?>) PluginContext.getContext()
                        .getPlugin(pi.getId()))
                .filter(p -> p.enable())
                .collect(Collectors.toList());
        return pluginList;
    }
    
    /**
     * @return
     */
    @Override
    public Set<String> getSocialLoginPluginIds() {
        Map<String, Object> params = new HashMap<>();
        params.put("category", "login");
        params.put("valid", "true");
        List<PluginInstance> piList = this.pluginInstanceService.queryList(true,
                params);
        Set<String> res = piList.stream()
                .map(pi -> pi.getId())
                .collect(Collectors.toSet());
        return res;
    }
    
    /**
     * @return
     */
    @Override
    public List<SocialAccountType> getSocialAccountTypes() {
        List<SocialLoginPlugin<?>> plugins = getSocialLoginPluginList();
        List<SocialAccountType> res = plugins.stream()
                .map(pi -> pi.getSocialAccountType())
                .collect(Collectors.toList());
        return res;
    }
    
    /**
     * @return
     */
    @Override
    public Set<String> getSocialAccountTypeCodes() {
        List<SocialLoginPlugin<?>> plugins = getSocialLoginPluginList();
        Set<String> res = plugins.stream()
                .map(pi -> pi.getSocialAccountType().getCode())
                .collect(Collectors.toSet());
        return res;
    }
    
    /**
     * @return
     */
    @Override
    public Map<String, SocialLoginPlugin<?>> getTypeCode2PluginMap() {
        List<SocialLoginPlugin<?>> plugins = getSocialLoginPluginList();
        Map<String, SocialLoginPlugin<?>> res = new HashMap<>();
        for (SocialLoginPlugin<?> p : plugins) {
            res.put(p.getSocialAccountType().getCode(), p);
        }
        return res;
    }
}
